﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcs.DAL
{
	/// <summary>
	/// 数据访问类:instore
	/// </summary>
	public partial class instore
	{
		public instore()
		{}
        /// <summary>
        /// 废旧物资库存入库查询(废旧库的名称定为：废旧回收物资库)
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataSet GetWaste(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM VwasteInstoreInventory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet Getinstore(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select spname,proid,usework, projectname,projecttype,intime,innum,uid,supplier");
            strSql.Append(" FROM Vinstore ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by intime desc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetinstoreByWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select spname,proid,usework,material,projectname,projecttype,intime,innum,uid,supplier,sname,iid,suppliername,supplierphone,unitPrice,unit,mid,purchaseid");
            strSql.Append(" FROM Vinstore Where 1=1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(strWhere);
            }
            strSql.Append(" order by intime desc");
            return DbHelperSQL.Query(strSql.ToString());
        }
        public int inst(int mid,int spid,int uid,float innum,int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into instore(");
            strSql.Append("mid,spid,uid,innum)");
            strSql.Append(" values (");
            strSql.Append("@mid,@spid,@uid,@innum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@spid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),					
					new SqlParameter("@innum", SqlDbType.Decimal,9)};
            parameters[0].Value = mid;
            parameters[1].Value = spid;
            parameters[2].Value = uid;
            parameters[3].Value =innum; 
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;               
            }
            else
            {
                //更新仓位状态
                spaceinfo sp = new spaceinfo();
                sp.UpdateStatus(spid, status);
                //更新库存
                inventory di = new inventory();
                int intd = di.Addin(mid, spid, innum);
                return intd;                
            }
        }
        public int inst(int mid, int spid, int uid, decimal innum, int status)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into instore(");
            strSql.Append("mid,spid,uid,innum)");
            strSql.Append(" values (");
            strSql.Append("@mid,@spid,@uid,@innum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@spid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),					
					new SqlParameter("@innum", SqlDbType.Decimal,9)};
            parameters[0].Value = mid;
            parameters[1].Value = spid;
            parameters[2].Value = uid;
            parameters[3].Value = innum;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                //更新仓位状态
                spaceinfo sp = new spaceinfo();
                sp.UpdateStatus(spid, status);
                //更新库存
                inventory di = new inventory();
                int intd = di.Addin(mid, spid, innum);
                return intd;
            }
        }
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("iid", "instore"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int iid)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from instore");
			strSql.Append(" where iid=@iid");
			SqlParameter[] parameters = {
					new SqlParameter("@iid", SqlDbType.Int,4)
			};
			parameters[0].Value = iid;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(lcs.Model.instore model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into instore(");
			strSql.Append("mid,spid,uid,intime,innum)");
			strSql.Append(" values (");
			strSql.Append("@mid,@spid,@uid,@intime,@innum)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@spid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@intime", SqlDbType.SmallDateTime),
					new SqlParameter("@innum", SqlDbType.Decimal,9)};
			parameters[0].Value = model.mid;
			parameters[1].Value = model.spid;
			parameters[2].Value = model.uid;
			parameters[3].Value = model.intime;
			parameters[4].Value = model.innum;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(lcs.Model.instore model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update instore set ");
			strSql.Append("mid=@mid,");
			strSql.Append("spid=@spid,");
			strSql.Append("uid=@uid,");
			strSql.Append("intime=@intime,");
			strSql.Append("innum=@innum");
			strSql.Append(" where iid=@iid");
			SqlParameter[] parameters = {
					new SqlParameter("@mid", SqlDbType.Int,4),
					new SqlParameter("@spid", SqlDbType.Int,4),
					new SqlParameter("@uid", SqlDbType.Int,4),
					new SqlParameter("@intime", SqlDbType.SmallDateTime),
					new SqlParameter("@innum", SqlDbType.Decimal,9),
					new SqlParameter("@iid", SqlDbType.Int,4)};
			parameters[0].Value = model.mid;
			parameters[1].Value = model.spid;
			parameters[2].Value = model.uid;
			parameters[3].Value = model.intime;
			parameters[4].Value = model.innum;
			parameters[5].Value = model.iid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int iid)
		{			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from instore ");
			strSql.Append(" where iid=@iid");
			SqlParameter[] parameters = {
					new SqlParameter("@iid", SqlDbType.Int,4)
			};
			parameters[0].Value = iid;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string iidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from instore ");
			strSql.Append(" where iid in ("+iidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.instore GetModel(int iid)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 iid,mid,spid,uid,intime,innum from instore ");
			strSql.Append(" where iid=@iid");
			SqlParameter[] parameters = {
					new SqlParameter("@iid", SqlDbType.Int,4)
			};
			parameters[0].Value = iid;

			lcs.Model.instore model=new lcs.Model.instore();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcs.Model.instore DataRowToModel(DataRow row)
		{
			lcs.Model.instore model=new lcs.Model.instore();
			if (row != null)
			{
				if(row["iid"]!=null && row["iid"].ToString()!="")
				{
					model.iid=int.Parse(row["iid"].ToString());
				}
				if(row["mid"]!=null && row["mid"].ToString()!="")
				{
					model.mid=int.Parse(row["mid"].ToString());
				}
				if(row["spid"]!=null && row["spid"].ToString()!="")
				{
					model.spid=int.Parse(row["spid"].ToString());
				}
				if(row["uid"]!=null && row["uid"].ToString()!="")
				{
					model.uid=int.Parse(row["uid"].ToString());
				}
				if(row["intime"]!=null && row["intime"].ToString()!="")
				{
					model.intime=DateTime.Parse(row["intime"].ToString());
				}
				if(row["innum"]!=null && row["innum"].ToString()!="")
				{
					model.innum=decimal.Parse(row["innum"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select iid,mid,spid,uid,intime,innum ");
			strSql.Append(" FROM instore ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" iid,mid,spid,uid,intime,innum ");
			strSql.Append(" FROM instore ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM instore ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.iid desc");
			}
			strSql.Append(")AS Row, T.*  from instore T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "instore";
			parameters[1].Value = "iid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

